﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
using Crosland.Automotriz.Logic;
using System.Diagnostics;

namespace Crosland.Automotriz.Web.UserControl
{
	public partial class MensajeError : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Errores(String msgUsuario, String msgReal, String fhError, String pagError, String metError, String codUsu,int Lineanumber, String titulo = "MENSAJE DE ERROR")
        {
            //General
            pcMensaje.HeaderText = titulo;
            lblMensajeCliente.Text = msgUsuario;

            pcMensaje.ShowOnPageLoad = true;

            //Detalles Tecnicos
            lblMsgReal.Text = msgReal;
            lblFHError.Text = fhError;
            lblPag.Text = pagError;
            lblMetodo.Text = metError;
            lblCodUsu.Text = codUsu;       
            lblLineaNumber.Text = Convert.ToString(Lineanumber);
            //envia correo 
           this.EnviaCorreo(msgUsuario,msgReal,fhError,pagError,metError,codUsu,Lineanumber);

        }

        private MailMessage mail = new MailMessage();

        private void EnviaCorreo(String msgUsuario, String msgReal, String fhError, String pagError, String metError, String codUsu,int linea) {
            try
            {
                string strMailSistema = ConfigurationManager.AppSettings["MailEnvioException"];
                mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["UserNameSMTP"]);
                mail.Subject = ("ERROR DEL SISTEMA INTRANET");
                mail.Body = String.Format(HtmlEmail.HtmlNotificacionSistemaExeption, "Revisar los siguientes errores.", msgUsuario, msgReal, fhError, pagError, metError, codUsu,linea);
                mail.IsBodyHtml = true;
                mail.To.Add(strMailSistema);
                SmtpClient s = new SmtpClient();
                s.Host = ConfigurationManager.AppSettings["EmailServer"];
                s.Port = 25;
                s.EnableSsl = false;
                s.UseDefaultCredentials = false;
                s.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["PasswordSMTP"]);
                s.Send(mail);   
            }
            catch (Exception e)
            {

                StackTrace trace = new StackTrace(e, true);
                int number = 0;
                for (int i = 0; i < trace.FrameCount; i++)
                {   StackFrame sf = trace.GetFrame(i);
                    number = sf.GetFileLineNumber();
                }
                lblLineaNumber.Text = Convert.ToString(number);
                lblMsgReal.Text = (e.InnerException != null) ? e.InnerException.Message : e.Message;
                lblPag.Text = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
                lblCodUsu.Text = Session["User"].ToString();
                lblFHError.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                lblMetodo.Text = "EnviaCorreo";
                lblMensajeCliente.Text = "No se ha podido enviar el E-Mail de errores al area de sistemas ";
                pcMensaje.ShowOnPageLoad = true;
            }           
     
        }
    }
}